<?php

namespace App\Controller\App;

use App\Base\BaseController4Restful;
use App\Repository\ConfigRepository;
use App\Service\MoneyService;
use App\Service\PayService;
use Symfony\Component\Routing\Annotation\Route;

class PayController extends BaseController4Restful
{
    /**
     * @Route("/app/pay/insuer", name="AppPayInsureMoney")
     */
    public function AppPayInsureMoney(ConfigRepository $repository,PayService $payService)
    {
        $user = $this->needToBeUser();
        if(!$payType = $this->getParamRequired("payType")){return $this->json_out(400,"请选择支付方式");}
        if(!$money = $this->getParamRequired("money")){return $this->json_out(400,"请选择支付金额");}

        //检查支付金额是否合法，是否在设置金额以内
        $config = $repository->findOneBy(array("k"=>"INSURE_MONEY"));
        $str = $config->getV();
        $arr = $this->myExplodeNumbers($str);
        if (!in_array($money,$arr)) {
            return $this->json_out(400,"支付金额不合法");
        }

        //检查支付金额是否合法
        if (!$payService->checkPayType( $payType)) {
            return $this->json_out(400,"支付方式不合法");
        }

        //创建支付回调订单
        $payOrder = $payService->createInsureMoneyOrder($user,$money);

        //创建支付参数
//        $param = $payService->createPayOrder($payOrder);
        $url = $this->generateUrl("AppPayCallBackCommonTest",array("sn"=>$payOrder->getSn()));
        $param = $url;
        return $this->json_out(200,"参数创建成功",["type"=>$payType,"param"=>$param]);
    }
}
